//
//  OrderCreateViewController.m
//  ZPIOSEducation
//
//  Created by Encoder on 17/2/17.
//  Copyright © 2017年 mini1. All rights reserved.
//

#import "OrderCreateViewController.h"
#import <AlipaySDK/AlipaySDK.h>
#import "ZPMineOrderViewController.h"
#define AliPayScheme @"AliPayForEdu"
#import "SubSucViewController.h"
@interface OrderCreateViewController (){
    MBProgressHUD *hud;
}
@property (weak, nonatomic) IBOutlet UILabel *needPayL;
@property (weak, nonatomic) IBOutlet UIButton *zhifubaoBtn;
@property (weak, nonatomic) IBOutlet UIButton *weixinBtn;
@property (weak, nonatomic) IBOutlet UIImageView *zhifubaoI;
@property (weak, nonatomic) IBOutlet UIImageView *weixinI;
@property (strong,nonatomic) NSMutableDictionary *dic;
@end

@implementation OrderCreateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"订单支付";
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(AliPaySuccess:) name:@"AliPayResult" object:nil];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"zhenpin_back"] style:UIBarButtonItemStylePlain target:self action:@selector(backButtonClick)];
    hud = [[MBProgressHUD alloc]initWithView:self.view];
    [self.view addSubview:hud];
    _dic = [NSMutableDictionary dictionary];
    _needPayL.text = [NSString stringWithFormat:@"¥%@",_needPay];
}
- (void)backButtonClick
{ if(_popBack){
    [self.navigationController popViewControllerAnimated:YES];
}else{
    ZPMineOrderViewController *vc=[[ZPMineOrderViewController alloc]init];
    vc.isFromPaySuc = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"AliPayResult" object:nil];
}

-(void)AliPaySuccess:(NSNotification *)noti{
    NSDictionary *resultDic=noti.userInfo;
    switch ([[resultDic objectForKey:@"resultStatus"] intValue]) {
        case 6001:
            [ZPPromptView showMessage:@"已取消支付"];
            break;
        case 9000:{
            SubSucViewController *vc=[[SubSucViewController alloc]init];
            vc.orderSn = _orderSn;
            vc.needPay = _needPay;
            vc.issuscess = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        default:
            break;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (IBAction)zhifubaoAc:(id)sender {
    _zhifubaoBtn.highlighted=YES;
    _zhifubaoI.highlighted=YES;
    _weixinBtn.highlighted=NO;
    _weixinI.highlighted=NO;
    
    [self zhifubaoPay];
    
    
}
- (IBAction)weixinAc:(id)sender {
    _zhifubaoBtn.highlighted=NO;
    _zhifubaoI.highlighted=NO;
    _weixinBtn.highlighted=YES;
    _weixinI.highlighted=YES;
    
    
    
    
    
}

-(void)zhifubaoPay{
    [hud showAnimated:YES];
    [_dic setObject:[[[ZPAuthStore sharedZPAuthStore] userToDic] objectForKey:@"token"] forKey:@"access_token"];
    [_dic setObject:[[[ZPAuthStore sharedZPAuthStore] userToDic] objectForKey:@"memberId"] forKey:@"memberId"];
    [_dic setObject:@"3" forKey:@"channel"];
    [_dic setObject:@"3" forKey:@"payTypeId"];
    [_dic setObject:_orderSn forKey:@"orderSn"];
    [[ZPNetworking sharedZPNetworking] GET:APIPay@"payall/pay.json" parameters:_dic progress:nil success:^(id  _Nonnull responseObject) {
        [hud hideAnimated:YES];
        if (CodeIs200(responseObject)) {
            [[AlipaySDK defaultService]payOrder:[responseObject objectForKey:@"result"] fromScheme:AliPayScheme callback:^(NSDictionary *resultDic) {
                NSLog(@"reslut = %@",resultDic);
                switch ([[resultDic objectForKey:@"resultStatus"] intValue]) {
                    case 6001:
                        [ZPPromptView showMessage:@"已取消支付"];

                        break;
                    case 9000:{
                        SubSucViewController *vc=[[SubSucViewController alloc]init];
                        vc.orderSn = _orderSn;
                        vc.needPay = _needPay;
                        vc.issuscess = YES;
                        [self.navigationController pushViewController:vc animated:YES];
                    }
                        break;
                    default:
                        break;
                }
            }];
            
        }else{
            [ZPPromptView showMessage:toStr([responseObject objectForKey:@"codeMsg"])];
            
        }
    } failure:^(NSError * _Nonnull error) {
        [ZPPromptView showMessage:error.localizedDescription];
        [hud hideAnimated:YES];
    }];
}

-(void)weixinPay{
    
}

@end
